<template>
    <div base>
        <!-- 轮播图 -->
        <div class="swiper">

        </div>
        <!-- 商品详情 -->
        <div class="shopinfo">
            <div>{{ getshopinfo.commodityinfo.name }}</div>
            <div>
                <span red f12>￥</span>
                <span f35 red>{{ getshopinfo.commodityinfo.Referenceprice }}</span>
            </div>
            <div class="parameter">
                <span class="description" v-for="item, index in getshopinfo.commodityinfo.parameter">
                    <span>{{ item.name }}: &nbsp;</span>
                    <span>{{ item.value }} </span>
                </span>
            </div>
        </div>
        <!-- 商品信息 -->
        <div class="Iteminformation">
            <!-- 头部 -->
            <div class="IteminformationTitle">
                <div>商品信息</div>
            </div>
            <!-- list -->
            <div class="IteminformationList">
                <div v-for="item, index in getshopinfo.commodityinfo.parameter">
                    <div>{{ item.name }}</div>
                    <div>{{ item.value }}</div>
                </div>
            </div>
        </div>
        <van-popup v-model:show="usestate.showpopup" position="bottom" :style="{ height: '50%' }">
            <div class="ordernow">
                <div class="ordernowtitle">
                    <div>立即下单</div>
                    <div>单价:{{ getshopinfo.commodityinfo.category.money }}元/件</div>
                </div>
                <!-- 表格 -->
                <div class="ordernowfrom">
                    <table width="100%" cellpadding="0" cellspacing="0" style="table-layout: fixed;text-align: center;">
                        <!-- 循环表头 -->
                        <tr style="background:#F7F7F7 ;;height: 35px;">
                            <td>
                                {{ getshopinfo.commodityinfo.category.name }}
                            </td>
                            <td v-for="item, index in getshopinfo.commodityinfo.category.Tableheader">
                                {{ item.key }}
                            </td>
                            <td>
                                小计
                            </td>
                        </tr>
                        <tr style="height: 35px;"
                            v-for="item, index in getshopinfo.commodityinfo.category.Productparameters">
                            <td>{{ item.name }}</td>
                            <td v-for="sum, index2 in item.shopinfo">
                                <van-field :placeholder="sum.number"
                                    style="width: 80%;;border: 0;text-align: center;border-bottom: 1px solid #ccc;margin: 0 5px;"
                                    v-model="sum.value" type="digit" input-align="center"
                                    @update:model-value="getsum(item, sum)" />
                            </td>
                            <td>{{ gettablemoney(item) }}</td>
                        </tr>

                    </table>
                </div>
                <div class="orderBottomfunc">
                    <div f12 marginleft5>合计:{{ getALLmoney("number") }}件</div>
                    <div>总价:<span f12 red>￥</span><span red>{{ getALLmoney("money") }}</span></div>
                    <div :class="[getALLmoney('number') > 0 ? 'red' : 'grey']" @click="Placeanorder">立即下单</div>
                </div>
            </div>
        </van-popup>
        <!-- 底部购买按钮 -->
        <PurchaseComponents @addShopCar="addShopCar" @BuyNOW="BuyNOW" @Service="Service"></PurchaseComponents>
    </div>
</template>
<script setup>
// 引入方法
import { ref, reactive, onMounted, computed, getCurrentInstance } from 'vue';

import { showToast } from 'vant'

// 引入组件
import PurchaseComponents from "@/components/shopinfo/PurchaseComponents.vue"
// 引入pinia
import { shop } from "@/state/index/shop.js"
const getshopinfo = shop()

// 生命周期
onMounted(() => {
    getshopinfo.getshopinfo().then(res => {
        console.log(JSON.parse(JSON.stringify(res)));
    })
})
const usestate = reactive({
    showpopup: false
})
// 购物车事件
const addShopCar = (e) => {
    console.log("addShopCar");
}
const gettablemoney = (e) => {
    let value = 0
    for (let i = 0; i < e.shopinfo.length; i++) {
        e.shopinfo[i].value ? value += Number(e.shopinfo[i].value) : ''
    }
    e.usershopNumber = value
    e.usershopMoney = Number(getshopinfo.commodityinfo.category.money) * value
    return e.usershopMoney
}
const BuyNOW = (e) => {
    console.log("BuyNOW");
    getshopinfo.getshopinfo().then(res => {
        usestate.showpopup = true
    })
}
const Service = () => {
    console.log("Service");
}
const getsum = (item, sum) => {
    console.log(item);
    console.log(sum);
    if (Number(sum.value) > Number(sum.number)) {
        showToast(item.name + getshopinfo.commodityinfo.category.name + sum.key + '超出最大值' + sum.number)
        sum.value = Number(sum.number)
    }
    getALLmoney()
}
const Placeanorder = () => {
    if (getALLmoney('number') > 0) {
        usestate.showpopup = false
        showToast("下单成功")
    }
}
const getALLmoney = (item) => {
    const Productparameters = getshopinfo.commodityinfo.category.Productparameters
    let money = 0
    let shopNumber = 0
    for (let i = 0; i < Productparameters.length; i++) {
        money += Productparameters[i].usershopMoney
        shopNumber += Productparameters[i].usershopNumber
    }

    if (item == "money") {
        return money
    } else {
        return shopNumber
    }
}
const clearInput = (e) => {
    console.log(e);
    let key = e.key;
    if (key === 'e' || key === 'E' || key === '+' || key === '-') {
        e.returnValue = false;
        return false;
    }
    return true;
}
</script>
<style lang="scss" scoped>
.swiper {
    height: 340px;
    background: #fff;
    border: 1px solid #ccc;

}

.shopinfo {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    color: #595959;
    background: #fff;
    padding-top: 8px;
    padding-bottom: 25px;

    >div:nth-child(2) {
        margin: 10px 0;
    }
}

.description:not(:last-child)::after {
    content: "|";
    margin: 0 2px;
}

.Iteminformation {
    width: 100%;
    background: #fff;
    margin-top: 10px;
    overflow: hidden;
    padding-bottom: 10px;
}

.IteminformationTitle {
    height: 35px;
    width: 100%;

    >div {
        height: 100%;
        margin: 10px 95px 0;
        display: flex;
        align-items: center;
        justify-content: center;
        background-color: #F7F7F7;
    }
}

.IteminformationList {
    margin: 0 10px;

    >div {
        display: flex;
        justify-content: space-between;
        border-bottom: 1px solid #F0F0F0;
        padding: 10px 0;

        >div:nth-child(2) {
            width: 80%;
        }

        >div:nth-child(1) {
            color: #A9A9A9;
        }
    }
}

.ordernow {
    overflow: scroll;
    height: 100%;
    position: relative;
}

.orderBottomfunc {
    display: flex;
    width: 100%;
    height: 45px;
    align-items: center;
    position: fixed;
    bottom: 0;
    background: #F7F7F7;
    justify-content: space-between;

    >div:nth-child(2) {
        flex: 2;
        display: flex;
        height: 200%;
        justify-content: flex-end;
        align-items: center;
        margin-right: 5px;
    }

    >div:nth-child(3) {
        width: 105px;
        background: #999999;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #fff;
    }
}

.ordernowtitle {
    display: flex;
    justify-content: space-between;
    margin: 0 15px;
    height: 35px;

    >div:nth-child(1) {
        font-size: 12px;
        height: 100%;
        display: flex;
        align-items: flex-end;

    }

    >div:nth-child(2) {
        color: #801E00;
        font-size: 22px;
        height: 100%;
        display: flex;
        align-items: flex-end;
    }
}

.ordernowfrom {
    width: 100%;
}

.red {
    background: red !important;
}

.grey {
    background: #999;
}
</style>